<?php

namespace App\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use voku\helper\HtmlDomParser;


class FoodsDataCatch3 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'food_data_catch3';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->scrawl_list();
        #$this->scraw_ips2();
        //$this->scrawl_category();
    }

    private function scrawl_category() {
        $client = new Client();
        $response = $client->request('get','http://food.boohee.com/fb/v1/categories/list',['header'=>[
            'App-Key'=>'food',
            'App-Version'=>'3.0.2',
            'App-Device'=>'ios',
            'User-Agent'=>'FoodLibrary/3/boohee',
            'Phone-Model'=>'iPhone',
            'Device_Id'=>'a700c098823c4568a938e5984b6dce18',
            'Os-Version'=>'13.6.1',
        ]])->getBody()->getContents();

        $ret = json_decode($response,true);
        $groups = $ret['group'][0]['categories'];
        foreach ($groups as $key=>$group) {
            $data = array(
                'name'=>$group['name'],
                'img'=>$group['image_url'],
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s'),
                'status'=>1
            );
            \DB::table('foods_categories1')->insert($data);
        }

    }

    private function scrawl_list() {
        $client = new Client();
        $cats = \DB::table('foods_categories')->get();
        foreach ($cats as $key=>$cat) {
            $link = 'http://food.boohee.com/fb/v1/foods?value='.$cat->id.'&order_by=1&kind=group&order_asc=0';
            $response = $client->request('get',$link,['header'=>[
                'App-Key'=>'food',
                'App-Version'=>'3.0.2',
                'App-Device'=>'ios',
                'User-Agent'=>'FoodLibrary/3/boohee',
                'Phone-Model'=>'iPhone',
                'Device_Id'=>'a700c098823c4568a938e5984b6dce18',
                'Os-Version'=>'13.6.1',
            ]])->getBody()->getContents();
            $ret = json_decode($response,true);
            $total_pages = $ret['total_pages'];
            $this->decode_data($ret,$cat->id,$link);

            for($i=2;$i<=10;$i++) {
                $link = 'http://food.boohee.com/fb/v1/foods?value='.$cat->id.'&order_by=1&kind=group&order_asc=0&page='.$i;
                $response = $client->request('get',$link,['header'=>[
                    'App-Key'=>'food',
                    'App-Version'=>'3.0.2',
                    'App-Device'=>'ios',
                    'User-Agent'=>'FoodLibrary/3/boohee',
                    'Phone-Model'=>'iPhone',
                    'Device_Id'=>'a700c098823c4568a938e5984b6dce18',
                    'Os-Version'=>'13.6.1',
                ]])->getBody()->getContents();
                $ret = json_decode($response,true);
                $this->decode_data($ret,$cat->id,$link);
            }
        }

    }

    private function decode_data($ret,$cat_id,$origin_link) {
        $foods= $ret['foods'];
        foreach ($foods as $key=>$food) {
            $origin_id = $food['id'];
            $code = $food['code'];
            $name = $food['name'];
            $img = $food['thumb_image_url'];

            $info = json_encode($food);
            \DB::table('foods_detail')->insert(compact('origin_id','code','name','img','cat_id','info','origin_link'));
        }
    }

}
